import pygame
from cls.enemy import Enemy

pygame.mixer.init()

class Human(pygame.sprite.Sprite):  # 通过精灵基类实例化
    speed = 7
    width, height = 1000, 600

    last = 0  # 用于保存上一个方向
    now = 0  #
    angle = 0  # 用于保存旋转的角度

    def __init__(self):
        super(Human, self).__init__()  # 调用父类的init
        self.life = 3  # 有三条命
        self.fireimage = [pygame.image.load("./pic/humanfire/humanfire{}.png".format(i))for i in range(1,4)]#开火图片
        self.imagegroupRightWithUp = [pygame.image.load("./pic/pic2/human{}.png".format(i)) for i in
                                      range(1, 15)]  # 加载人物图片
        self.imagegroupLeft = [pygame.image.load("./pic/pic2/human{}.png".format(i)) for i in range(1, 15)]  # 加载人物图片
        self.image = self.imagegroupRightWithUp[0]
        self.rect = self.image.get_rect()  # 获得人物图片的外边框
        self.rect.x = self.width / 2 - self.rect.width / 2  # 初始化人物的位置
        self.rect.y = self.height / 2 - self.rect.height  # 初始化人物的位置
        self.index = 0  # 第一张图
        self.index2 = 0 #子弹发射的图
        self.humanboom = pygame.mixer.Sound("./music/humanboom.wav")#人中弹


    # 撞击后生命值扣一
    def scrike(self, enemy_group):
        scrikegroup = pygame.sprite.spritecollide(self, enemy_group, True)
        if len(scrikegroup) > 0:
            self.humanboom.play()
            self.life -= 1

    # 判断是否存活
    def is_survive(self):
        return self.life > 0

    # 移动函数，并将人物限制在屏幕中
    def move_up(self):
        if(Human.last == 0):
            self.image = self.imagegroupLeft[self.index % 14]
        else:
            self.image = self.imagegroupRightWithUp[self.index % 14]
            self.turn_around(1, 0)
        self.index += 1
        if self.rect.y > 0:
            self.rect.y -= self.speed
        else:
            self.rect.y = 0

    def move_down(self):
        if (Human.last == 2):
            self.image = self.imagegroupRightWithUp[self.index % 14]
        else:
            self.image = self.imagegroupLeft[self.index % 14]
            self.turn_around(1, 0)
        self.index += 1
        if self.rect.y < self.height - self.rect.height:
            self.rect.y += self.speed
        else:
            self.rect.y = self.height - self.rect.height

    def move_left(self):
        self.image = self.imagegroupLeft[self.index % 14]
        self.turn_around(1, 0)
        if self.last != 2 and self.now == 2:
            self.turn_around(1, 0)
        self.index += 1
        if self.rect.x > 0:
            self.rect.x -= self.speed
        else:
            self.rect.x = 0

    def move_right(self):
        self.image = self.imagegroupRightWithUp[self.index % 14]
        if self.last != 0 and self.now == 0:
            self.turn_around(1, 0)
        self.index += 1
        if self.rect.x < self.width - self.rect.width:
            self.rect.x += self.speed
        else:
            self.rect.x = self.width - self.rect.width

    # 子弹发射的动作
    def fireB(self):
        if self.now == 0:
            self.image = self.fireimage[self.index2%3]
        elif self.now ==2:
            self.image = pygame.transform.flip(self.fireimage[self.index2%3], 1, 0)
        self.index2 += 1
    # 人物转向
    def turn_around(self, x, y):
        self.image = pygame.transform.flip(self.image, x, y)
